#!/usr/bin/env python
# coding=utf-8

import os
import codecs
import re
import time
from glob import glob

mp3FilePatterns = (u'*.mp3',)
listFilename = u'mp3list.txt'


def main():
	mp3Filenames = []
	for p in mp3FilePatterns:
		mp3Filenames += glob(p)
	mp3Renames = []
	mp3Index = 1
	for f in mp3Filenames:
		if re.match(r'\d+\.\S+', f):
			mp3Renames.append((f, f))
		else:
			rf = str(mp3Index) + f[f.rfind('.'):]
			mp3Index += 1
			while os.path.exists(rf):
				rf = str(mp3Index) + f[f.rfind('.'):]
				mp3Index += 1
			mp3Renames.append((f, rf))
	listFile = codecs.open(listFilename, 'a', 'utf-8')
	listFile.write('\n')
	listFile.write(time.asctime() + '\n')
	for f,rf in mp3Renames:
		os.rename(f, rf)
		listFile.write(u'%s	-->	%s\n' % (f.decode('gbk'), rf))
		print(u'%s	-->	%s' % (f.decode('gbk'), rf))

if __name__ == '__main__':
	main()


